﻿using BySoft.Server.Business;
using BySoft.Server.Business.UserCode;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class QuerySProductByDeliveryOrder : BasePage
    {
        int m_count = 0;
        string m_strCurrentID="";
        string m_strCustomerID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                m_strCurrentID = UIHelper.GetQueryStringValue("id");
                m_strCustomerID = UIHelper.GetQueryStringValue("customerID");
                BindData(0);
            }
        }
        private DataTable DtDeliveryOrderData
        {
            get
            {
                if (null == Session["DeliveryOrderData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["DeliveryOrderData"];
                }
            }
            set
            {
                Session["DeliveryOrderData"] = value;
            }
        }

        private DataTable CreateDT()
        {

            return BsDeliveryorder.CreateDetailDT();
        }
        private void BindData(int pageIndex)
        {

           
        
            DataTable dt = BsDeliveryorder.SelectDialogList(m_strCurrentID, txtProductName.Text.Trim(),
                            txtOrderID.Text.Trim(), m_strCustomerID, pageIndex, pcData.PageSize, out m_count);
            pcData.RecordCount = m_count;
            if (dt.Rows.Count != 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
                EnableGridViewRow(grdResult, dt, "RowNumber", false);
                pcData.CurrentIndex = pageIndex;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

       

        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cb = (CheckBox)e.Row.FindControl("cbChoose");
                cb.Attributes.Add("onclick", "judge(this)");
            }
        }

        private void AddPro()
        {
            DtDeliveryOrderData = GetAddPro(DtDeliveryOrderData).Copy();
        }

        private DataTable GetAddPro(DataTable dt)
        {
            DataTable dtTemp = dt.Copy();
            foreach (GridViewRow gvr in grdResult.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    Label lblRowNumber = gvr.FindControl("lblRowNumber") as Label;
                    Label lblProductID = gvr.FindControl("lblProductID") as Label;
                    Label lblProductName = gvr.FindControl("lblProductName") as Label;
                    Label lblSpecifications = gvr.FindControl("lblSpecifications") as Label;
                    Label lblUnit = gvr.FindControl("lblUnit") as Label;
                    Label lblCustomerPartNo=gvr.FindControl("lblCustomerPartNo") as Label;
                    Label lblOutSide = gvr.FindControl("lblOutSide") as Label;
                    Label lblInSide = gvr.FindControl("lblInSide") as Label;
                    Label lblLastNum = gvr.FindControl("lblLastNum") as Label;
                    Label lblNums = gvr.FindControl("lblLastNum") as Label;
                    Label lblPrice = gvr.FindControl("lblPrice") as Label;
 

                    if (((CheckBox)gvr.FindControl("cbChoose")).Checked && gvr.Enabled)
                    {
                        //不能用产品编号唯一标识
                        DataRow[] drs = dtTemp.Select("RowNumber='" + lblRowNumber.Text.Trim() + "'  ");
                        if (drs.Length == 0)
                        {
                            DataRow dr = dtTemp.NewRow();
                            dr["ProductID"] = lblProductID.Text.Trim();
                            dr["ProductName"] = lblProductName.Text.Trim();
                            dr["Specifications"] = lblSpecifications.Text.Trim();
                            dr["Unit"] = lblUnit.Text.Trim();
                            dr["CustomerPartNo"] = lblCustomerPartNo.Text.Trim();
                            dr["OutSide"] = lblOutSide.Text.Trim();
                            dr["InSide"] = lblInSide.Text.Trim();
                            dr["LastNum"] = lblLastNum.Text.Trim();
                            dr["Nums"] = lblNums.Text.Trim();
                            dr["RowNumber"] = lblRowNumber.Text.Trim();
                            dr["Price"] = lblPrice.Text.Trim();

                            dr["IsDel"] = "-1";
                            dr["IsNew"] = "1";
                            dtTemp.Rows.Add(dr);
                        }
                        else if (drs.Length > 0 && BySoft.Server.Common.CommonMethod.GetString(drs[0]["IsDel"]) == "1")
                        {
                            drs[0]["ProductID"] = lblProductID.Text.Trim();
                            drs[0]["ProductName"] = lblProductName.Text.Trim();
                            drs[0]["Specifications"] = lblSpecifications.Text.Trim();
                            drs[0]["Unit"] = lblUnit.Text.Trim();
                            drs[0]["CustomerPartNo"] = lblCustomerPartNo.Text.Trim();
                            drs[0]["OutSide"] = lblOutSide.Text.Trim();
                            drs[0]["InSide"] = lblInSide.Text.Trim();
                            drs[0]["LastNum"] = lblLastNum.Text.Trim();
                            drs[0]["Nums"] = lblNums.Text.Trim();
                            drs[0]["RowNumber"] = lblRowNumber.Text.Trim();
                            drs[0]["Price"] = lblPrice.Text.Trim();

                            drs[0]["IsDel"] = "-1";
                        }
                    }
                    else
                    {
                        DataRow[] drs = dtTemp.Select("RowNumber='" + lblRowNumber.Text.Trim() + "'  ");
                        if (drs.Length > 0)
                        {
                            drs[0]["IsDel"] = "1";
                        }
                    }
                }
            }
            return dtTemp;
        }
        protected void btnOk_Click(object sender, EventArgs e)
        {
            AddPro();
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'success');CloseTopDialog();</script>");
        }

        protected void pcData_PageIndexClick(object sender, Controls.PageIndexClickEventArgs e)
        {
            AddPro();
            BindData(e.PageIndex);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AddPro();
            BindData(0);
        }
    }
}